from AGUtils import *
from AGDefines import *


import logging
import time
from AGArea import *
logger = logging.getLogger(__name__)


class PublicAct():

    def __init__(self):
        self.areaDict = {}
        self.name = "Public"
        loadActions(self.name, self.areaDict, publicActions)

        self.ShangDianDict = {}
        loadActions(self.name, self.ShangDianDict, ShangDianActions)


    def detectZhanDou(self, img, lianXuFeiZhanDou = 0, lianXuYiChang = 0):

        ret = STATUS_ZHANDOU
        if self.areaDict['ZhanDouZiDong'].detect(img) < 10:
            lianXuFeiZhanDou = 0
            lianXuYiChang = 0
            ret = STATUS_ZHANDOU
        elif self.areaDict['ZhanDouShouDong'].detect(img) < 10:
            lianXuFeiZhanDou = 0
            lianXuYiChang = 0
            ret = STATUS_ZHANDOU_SHOUDONG
        elif self.areaDict['FeiZhanDou'].detect(img) < 10:
            lianXuFeiZhanDou += 1
            lianXuYiChang = 0
            ret = STATUS_FEIZHANDOU
        else:
            lianXuFeiZhanDou = 0
            lianXuYiChang += 1
            ret = STATUS_OTHERS


        return ret, lianXuFeiZhanDou, lianXuYiChang

    def closeWindow(self, enum, img):
        if self.areaDict['CloseKuang1'].detect(img) < 20:
            enum.click(self.areaDict['CloseKuang1'].click)
        else:
            enum.click(Center)

    def IsWuPinLan(self, img):
        if self.areaDict['WuPinLan'].detect(img) < 10:
            logger.info("当前是 物品栏 界面")
            return STATUS_OK
        else:
            logger.info("当前不是 物品栏 界面")
            return STATUS_OTHERS

    def checkShangDian(self, enum, img):

        for key in self.ShangDianDict:
            if self.ShangDianDict[key].detect(img) < 10:
                if(key == 'BaiTan'):
                    enum.click((225, 108, 'Select First'))
                enum.click(self.ShangDianDict[key].click)
                return STATUS_OK

        return STATUS_NOTFOUND

    def detectShiYong(self, enum, img):

        if self.areaDict['ShiYong'].detect(img) < 10:
            enum.click(self.areaDict['ShiYong'].click)

gPublic = PublicAct()